package indexer;

import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import databaseconnector.DBProcessor;

public class SecondHand_index {

	//MySQL数据库连接器
		private static DBProcessor processor;
		
		//构造函数
		public SecondHand_index()
		{
			processor = new DBProcessor();
		}

		//从SecondHand数据库抽出数据建立索引
		public static void buildSecondHandIndex(boolean create) throws IOException 
		{
			//索引输出目录
			Directory dir = FSDirectory.open(new File("E:/Index/SecondHandIndex"));
			
			IndexWriterConfig cfg = new IndexWriterConfig(Version.LUCENE_40,
					new SmartChineseAnalyzer(Version.LUCENE_40));
			
			if (create) {
				cfg.setOpenMode(OpenMode.CREATE);
			} else {
				cfg.setOpenMode(OpenMode.CREATE_OR_APPEND);
			}
			
			IndexWriter writer = new IndexWriter(dir, cfg);
			ResultSet rlt = processor.getSecondHand();
			Document doc = new Document();
			
			
			//Field域构建
			Field id = new StoredField("id", Integer.MIN_VALUE);
			
			//房屋总价(万)
			Field price = new IntField("price",0,Field.Store.YES);
			
			//房屋单价（元/平方米）
			Field price_detail = new IntField("price_detail",0,Field.Store.YES);
			
			//首付（万元）
			Field price_first = new IntField("price_detail",0,Field.Store.YES);
			
			//月供（元）
			Field price_month = new IntField("price_detail",0,Field.Store.YES);
			
			//几室几厅
			Field room = new IntField("room",0,Field.Store.YES);
			Field hall = new IntField("hall",0,Field.Store.YES);
			
			Field decoration = new TextField("decoration","", Field.Store.YES);
			
			//房屋面积（平方米）
			Field area = new IntField("area",0,Field.Store.YES);
			
			//房屋初始年限
			Field year = new IntField("year",0,Field.Store.YES);
			
			//将stair信息分成“高、中、低”楼层信息和具体楼层数信息
			Field stair = new TextField("stair", "", Field.Store.YES);
			Field stair_num = new IntField("stair_num", 0, Field.Store.YES);
			
			Field build = new TextField("build", "", Field.Store.YES);
			Field direction = new TextField("direction", "", Field.Store.YES);
			Field region = new TextField("region", "", Field.Store.YES);
			Field imglink = new StoredField("imglink", "");
			
			
			//将Field加入到doc中
			doc.add(id);
			doc.add(price);
			doc.add(price_detail);
			doc.add(price_first);
			doc.add(price_month);
			doc.add(room);
			doc.add(hall);
			doc.add(decoration);
			doc.add(area);
			doc.add(year);
			doc.add(stair);
			doc.add(stair_num);
			doc.add(build);
			doc.add(direction);
			doc.add(region);
			doc.add(imglink);
			

			//为doc的各Field赋值
			try {
				while (rlt.next()) {
					
					//设置Id值
					id.setIntValue(rlt.getInt(1));
					System.out.print(rlt.getInt(1) + " ");
					
					
					//设置price值
					String tempPrice = rlt.getString(2);
					try{
						tempPrice = tempPrice.replaceAll("万", "").trim();
						price.setIntValue(Integer.parseInt(tempPrice));
						System.out.print(Integer.parseInt(tempPrice) + " ");
					}
					catch(Exception e){}
					
					//设置房屋售价详细信息
					String priceDetail = rlt.getString(3);
					int tempPriceDetail = 0;
					int tempPriceFirst = 0;
					int tempPriceMonth = 0;
					try{
						if(priceDetail.contains("元/平米"))
						{
							tempPriceDetail = Integer.parseInt(priceDetail.substring(0, 
									priceDetail.indexOf("元/平米")).trim());
							price_detail.setIntValue(tempPriceDetail);
						}
						if(priceDetail.contains("首付"))
						{
							int index1 = priceDetail.indexOf("首付：") + 3;
						    int index2 = priceDetail.indexOf("万",index1);
							tempPriceFirst = Integer.parseInt(priceDetail.substring(index1,index2));
							price_first.setIntValue(tempPriceFirst);
						}
						if(priceDetail.contains("月供"))
						{
							int index1 = priceDetail.indexOf("月供：") + 3;
						    int index2 = priceDetail.indexOf("元",index1);
							tempPriceMonth = Integer.parseInt(priceDetail.substring(index1,index2));
							price_month.setIntValue(tempPriceMonth);
						}
						System.out.print(tempPriceDetail +  " " + tempPriceFirst + " " + tempPriceMonth + " ");
					}
					catch(Exception e){}
					
					//设置房间信息
					String tempHouse = rlt.getString(4);
					int tempRoom = 0;
					int tempHall = 0;
					if(tempHouse.contains("室"))
					{
						tempRoom = Integer.parseInt(tempHouse.substring(0, tempHouse.indexOf("室")));
						room.setIntValue(tempRoom);
						if(tempHouse.contains("厅"))
						{
							tempHall = Integer.parseInt(tempHouse.substring(tempHouse.indexOf("室")+1,
									tempHouse.indexOf("厅")));
							hall.setIntValue(tempHall);
						}
					}
					System.out.print(tempRoom + " " + tempHall + " ");
					
					//设置装修状况信息
					String tempDecoration = rlt.getString(5);
					if(tempDecoration != null)
					{
						decoration.setStringValue(tempDecoration);
						System.out.print(tempDecoration + " ");
					}
					
					//设置area值
					String tempArea = rlt.getString(6);
					if(tempArea.contains("平米"))
					{
						int tempAreaNum = Integer.parseInt(tempArea.substring(0,
								tempArea.length() - 2));
						area.setIntValue(tempAreaNum);
						System.out.print(tempAreaNum + " ");
					}
					
					//设置year值
					String tempYear = rlt.getString(7);
					if(tempYear.contains("年"))
					{
						int tempYearNum = Integer.parseInt(tempYear.substring(0,
								tempYear.length() - 1));
						year.setIntValue(tempYearNum);
						System.out.print(tempYearNum + " ");
					}
					
					//设置stair值
					String tmpStr = rlt.getString(8);
					if(tmpStr != null)
					{
						String tempStair = tmpStr.substring(0, tmpStr.indexOf("楼层"));
						stair.setStringValue(tempStair);
						if(tmpStr.contains("楼层/"))
						{
							int front = tmpStr.indexOf("楼层/") + 3;
							int rear = tmpStr.indexOf("层", front);
							int tempStairNum = Integer.parseInt(tmpStr.substring(front, rear));
							stair_num.setIntValue(tempStairNum);
							System.out.print(tempStair + " " + tempStairNum + " ");
						}
					}
					
					//设置build值
					String tempBuild = rlt.getString(9);
					if(tempBuild != null){
						build.setStringValue(tempBuild);
						System.out.print(tempBuild + " ");
					}
					
					//设置direction值
					String tempDirection = rlt.getString(10);
					if(tempDirection != null){
						direction.setStringValue(tempDirection);
						System.out.print(tempDirection + " ");
					}
					
					//设置Region值
					String tempRegion = rlt.getString(11);
					if(tempRegion != null){
						region.setStringValue(tempRegion);
						System.out.print(tempRegion + " ");
					}
					
					//设置imglink值
					String tempImglink = rlt.getString(13);
					if(tempImglink != null){
						imglink.setStringValue(tempImglink);
						System.out.print(tempImglink + " ");
					}
					
					System.out.println("");
					writer.addDocument(doc);

				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (create) {
				writer.forceMerge(1);
			}
			writer.close();
		}
}
